﻿// <copyright file="MostPopularItem.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-09-13</date>
// <summary>Most Popular item CLR Class definition</summary>

namespace NYTimes.Data
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Media;

    /// <summary>
    /// An item returned from the MostPopular API
    /// </summary>
    public class MostPopularItem
    {
        /// <summary>
        /// The thumbnail image
        /// </summary>
        private ImageSource thumbnail;

        /// <summary>
        /// Gets or sets the item's URL
        /// </summary>
        public Uri Url { get; set; }

        /// <summary>
        /// Gets or sets the column
        /// </summary>
        public string Column { get; set; }

        /// <summary>
        /// Gets or sets the section
        /// </summary>
        public string Section { get; set; }

        /// <summary>
        /// Gets or sets the byline
        /// </summary>
        public string Byline { get; set; }

        /// <summary>
        /// Gets or sets the title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the article abstract
        /// </summary>
        public string Abstract { get; set; }

        /// <summary>
        /// Gets or sets the published date
        /// </summary>
        public DateTime PublishedDate { get; set; }

        /// <summary>
        /// Gets or sets the source
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// Gets or sets the Description facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Not for serialization.")]
        public Collection<string> DescriptionFacets { get; set; }

        /// <summary>
        /// Gets or sets the organization facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Not for serialization.")]
        public Collection<string> OrgFacets { get; set; }

        /// <summary>
        /// Gets or sets the person facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Not for serialization.")]
        public Collection<string> PerFacets { get; set; }

        /// <summary>
        /// Gets or sets the geographic facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Not for serialization.")]
        public Collection<string> GeoFacets { get; set; }

        /// <summary>
        /// Gets or sets the media items
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Not for serialization.")]
        public Collection<MostPopularMediaItem> Media { get; set; }

        /// <summary>
        /// Gets the thumbnail image source
        /// </summary>
        public ImageSource Thumbnail
        {
            get
            {
                if (this.thumbnail == null)
                {
                    var query = from mediaItem in this.Media
                                from metadata in mediaItem.Metadata
                                where metadata.Format == "Standard Thumbnail"
                                select metadata.Image;

                    this.thumbnail = query.FirstOrDefault();
                }

                return this.thumbnail;
            }
        }
    }
}
